package likou.offer;

import java.util.*;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/8/16  10:35
 */
public class _32 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(3);
        treeNode.left=new TreeNode(9);
        treeNode.right=new TreeNode(20);
        treeNode.right.left=new TreeNode(15);
        treeNode.right.right=new TreeNode(7);
        int w[] = levelOrder(treeNode);
        for (int i=0;i<w.length;i++){
            System.out.println(w[i]);
        }
    }
    public static int[] levelOrder(TreeNode root) {
        if(root==null){
            return new int[0];
        }
        List<TreeNode> listA = new ArrayList<>();
        List<Integer> daan = new ArrayList<>();
        listA.add(root);
        while(true){
            List<TreeNode> listB = new ArrayList<>();
            for(TreeNode now:listA){
                if(now.left!=null){
                    listB.add(now.left);
                }
                if(now.right!=null){
                    listB.add(now.right);
                }
            }
            listA = listB;
            for(TreeNode x:listB){
                daan.add(x.val);
            }
            if(listB.size()==0){
                break;
            }
        }
        int res[] = new int[daan.size()+1];
        if(res.length>0){
            res[0] = root.val;
        }
        for(int i=1;i<=daan.size();i++){
            res[i] = daan.get(i-1);
        }
        return res;
    }
}
